<?php

class Sistema_Form_Fornecedor extends Zend_Form
{

    public function init()
    {
        $front = Zend_Controller_Front::getInstance();
        $controller = $front->getRequest()->getControllerName();
        $module = $front->getRequest()->getModuleName();
        $url = $front->getBaseUrl().'/'.$module.'/'.$controller;
        
        $this->setName('form_'.$controller);
        $this->setAttrib('class', 'formCtrl');
        
        $id = $front->getRequest()->getParam('id',0);
        
        $el = new Zend_Form_Element_Hidden('FOR_ID',array('disableLoadDefaultDecorators' =>true));
        $el->addDecorators(array(array('ViewHelper'),array('Errors')))
            ->addFilter('Int');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Text('FOR_NOM');
        $el->setLabel('Nome')
        ->setRequired(true)
        ->addFilters(array('StripTags','StringTrim'))
        ->addValidator('NotEmpty');
        $el->setAttrib('required', '');
        $this->addElement($el);
        
        $html = '';
        $htmli = '';
        $obj = new Sistema_Model_DbTable_Catsegmento();
        $row = $obj->fetchAll($obj->select()->order('CTS_ORD ASC'))->toArray();
        $arr = array();
        $arr_set = array();
        
        foreach($row as $r){
            
            $count = 0;
            $checked = '';
            
            $fsc = new Sistema_Model_DbTable_FornecedorHasSetcat();
            $fsc = $fsc->fetchAll('FOR_ID = '.$id.' and CTS_ID = '.$r['CTS_ID'])->toArray();
            foreach($fsc as $f){
                $arr_set[$f['CAT_ID']] = $f['CAT_ID'];
                $count = 1;
            }
            
            $htmli = '';
            if($count == 1){
                
                $checked = 'checked=""';
                
                $htmli = '<select id="SEG_CAT['.$r['CTS_ID'].'][]" name="SEG_CAT['.$r['CTS_ID'].'][]" class="chosen-select" multiple="multiple" style="width:500px;">';
            
                $cat = new Sistema_Model_DbTable_Categoria;
                //$cat = $cat->fetchAll('CTS_ID = '.$r['CTS_ID']);
                $cat = $cat->fetchAll($cat->select()->where('CTS_ID = '.$r['CTS_ID'])->order('CAT_NOM ASC')); 

                foreach($cat as $c){ //$arr[$r['MTL_ID']] = $r['MAT_NOM']; 
                    $selected = array_search($c['CAT_ID'], $arr_set) > '' ? 'selected=""':'';            
                    $htmli .= '<option '.$selected.' value="'.$c['CAT_ID'].'">'.$c['CAT_NOM'].'</option>'; 
                }
                $htmli .= '</select><br><br>';
            }
            
            $html .= '<div><label><input type="checkbox" '.$checked.' did="cat'.$r['CTS_ID'].'" seg="'.$r['CTS_ID'].'" class="seg_ck" name="seg'.$r['CTS_ID'].'" id="seg'.$r['CTS_ID'].'" value="'.$r['CTS_ID'].'"> '.$r['CTS_NOM'].'</label>';
            $html .= '<div id="cat'.$r['CTS_ID'].'">'.$htmli.'</div></div>';
        }
        
        $el = new Zend_Form_Element('SEG_IDS',array( 'helper'=>'formNote', 'value'=> $html ));
        $el->setLabel('Segmento');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Checkbox('FOR_STA',array('checked'=>'checked'));
        $el->setLabel('Status');
        $this->addElement($el);
        
        $btn = new Zend_Form_Element_Submit('submit',array('disableLoadDefaultDecorators' =>true));
        $btn->setLabel('Gravar');
        $btn->addDecorators(array(array('ViewHelper'),array('Errors')));
        $btn->setAttrib('id', 'submitbutton');
        $btn->setAttrib('class', 'btn-button btn-ok');
        $this->addElement($btn);
        
        $btn = new Zend_Form_Element_Button('Cancelar',array('disableLoadDefaultDecorators' =>true));
        //$btn->addDecorators(array(array('ViewHelper'),array('Errors')));
        $btn->setDecorators(array(array('Description', array('escape' => false, 'tag' => false))));
        $btn->setDescription('<a href="'.$url.'" class="btn-button btn-cancel">Cancelar</a>');
        $btn->setAttrib('id', 'btncancel');
        $btn->setAttrib('class', 'btn-button btn-cancel');
        $this->addElement($btn);
        
    }


}

